package leetcode.code167.TwoSumII;

/**
 * @author Administrator
 *
 */
public class Solution1 implements ITwoSumII {

	@Override
	public int[] twoSum(int[] numbers, int target) {
		int l = 0;
		int r = numbers.length - 1;
		while (l < r) {
			if (numbers[l] + numbers[r] == target) {
				break;
			}
			if (numbers[l] + numbers[r] < target) {
				l++;
			} else if (numbers[l] + numbers[r] > target) {
				r--;
			}
		}
		return new int[] { l + 1, r + 1 };
	}

	private void debug(int[] numbers, int target) {
		ITwoSumII its = new Solution1();
		int[] result = its.twoSum(numbers, target);
		System.out.println(result[0] + "," + result[1]);
	}

	public void debug1() {
//		���룺numbers = [2,7,11,15], target = 9
//		�����[1,2]
//		���ͣ�2 �� 7 ֮�͵���Ŀ���� 9 ����� index1 = 1, index2 = 2 ��
		int[] numbers = { 2, 7, 11, 15 };
		int target = 9;
		System.out.println("[1,2]");
		this.debug(numbers, target);
	}

	public void debug2() {
//		���룺numbers = [2,3,4], target = 6
//		�����[1,3]
		int[] numbers = { 2, 3, 4 };
		int target = 6;
		System.out.println("[1,3]");
		this.debug(numbers, target);
	}

	public void debug3() {
//		���룺numbers = [-1,0], target = -1
//		�����[1,2]
		int[] numbers = { -1, 0 };
		int target = -1;
		System.out.println("[1,2]");
		this.debug(numbers, target);
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
	}

}
